import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/LayoutContainer.vue'),
      redirect: '/login',
      children: [
        {
          path: '/avatar',
          name: 'avatar',
          component: () => import('../views/user/UserAvatar.vue')
        },
        {
          path: '/password',
          name: 'password',
          component: () => import('../views/user/UserPassword.vue')
        },
        {
          path: '/profile',
          name: 'profile',
          component: () => import('../views/user/UserProfile.vue')
        },
        {
          path: '/channel',
          name: 'channel',
          component: () => import('../views/article/ArticleChannel.vue')
        },
        {
          path: '/manage',
          name: 'manage',
          component: () => import('../views/article/ArticleManage.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginPage.vue')
    }
  ]
})
/**
 * 路由前置守卫
 * @param to
 * @returns
 */
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})
export default router
